#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Datetime: 2023/4/6 15:28
# @Author  : CHENWang
# @Site    : 
# @File    : alternative_me.py
# @Software: PyCharm

"""
脚本说明:
"""

import time
import numpy as np
import pandas as pd
import requests
import json
from retrying import retry
import os
from quant_researcher.quant.project_tool.time_tool import get_today, calc_date_diff
from quant_researcher.quant.project_tool.localize import DATA_DIR


@retry(stop_max_attempt_number=3)
def get_greed_fear_index():
    """

    :return:
    """

    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36',
    }

    url = f'https://api.alternative.me/fng/?limit=0&format=json&date_format=cn'

    response = requests.request("GET", url, headers=header, timeout=15)
    res = json.loads(response.text)['data']
    res_df = pd.DataFrame(res)
    res_df.set_index('timestamp', inplace=True)
    res_df.sort_index(inplace=True)
    res_df['value'] = res_df['value'].astype(int)

    return res_df


if __name__ == '__main__':
    df = get_greed_fear_index()

    from quant_researcher.quant.datasource_fetch.crypto_api.glassnode import get_prices
    price_log_price = get_prices(ohlc=False, asset='BTC', start_date='2015-01-01', end_date=None)
    price_log_price['log_prices'] = np.log10(price_log_price['close'])
    all_frame = df.merge(price_log_price, left_index=True, right_index=True)
    all_frame.sort_index(inplace=True)

    file_name = os.path.join(DATA_DIR, f'greed_fear_index')
    all_frame.to_excel(f'{file_name}.xlsx')

